package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Banner;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.query.BannerQuery;
import cn.wolfcode.wolf2w.repository.BannerRepository;
import cn.wolfcode.wolf2w.service.IBannerService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.sql.PseudoColumnUsage;
import java.util.List;

@Service
public class BannerServiceImpl implements IBannerService {
    @Value("$(sms.url)")
    private String smsUrl;
    @Value("$(sms.appkey)")
    private String appkey;

    @Autowired
    private MongoTemplate template;

    @Autowired
    private BannerRepository repository;
    @Autowired
    private IStrategyService strategyService;

    @Override
    public void save(Banner banner) {
        banner.setId(null);
        repository.save(banner);
    }

    @Override
    public void update(Banner banner) {
        repository.save(banner);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public Banner get(String id) {
        Banner banner =repository.findById(id).orElse(null);
        return banner;
    }

    @Override
    public List<Banner> list() {
        return repository.findAll();
    }

    @Override
    public Page<Banner> query(BannerQuery qo) {
        //分页查询
        //查询对象
        //1: 查询总数:total
        Query query = new Query();
        long total = template.count(query, Banner.class);
        if(total == 0){
            return Page.empty();  //空的分页对象
        }
        //2: 每页显示条数集合: list
        //设置页面显示条数, 还有当前页
        //1 当前页 2 每页的条数 3 排序规则 4 排序字段
        //分页参数对象
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.ASC, "_id");
        query.with(pageable);
        List<Banner> list = template.find(query, Banner.class);

        //3:封装page对象
        return new PageImpl<Banner>(list,pageable,total);
    }

    @Override
    public void saveOrUpdate(Banner banner) {
        if (!(StringUtils.hasLength(banner.getId()))){
            banner.setId(null);
            this.save(banner);
        }else {
            this.update(banner);

        }
    }

    @Override
    public List<Banner> findByTop5(int type) {
        //关于排序都要PageRequest
        PageRequest pageRequest = PageRequest.of(0, 5, Sort.Direction.ASC, "sequence");
        List<Banner> list = repository.findByStateAndType(Banner.STATE_NORMAL,Banner.TYPE_TRAVEL,pageRequest);
        return list;
    }

    @Override
    public List<Banner> findByTop1(int typeStrategy) {
        //关于排序都要PageRequest
        PageRequest pageRequest = PageRequest.of(0, 5, Sort.Direction.ASC, "sequence");
        List<Banner> list = repository.findByStateAndType(Banner.STATE_NORMAL,Banner.TYPE_STRATEGY,pageRequest);
        return list;
    }


}
